package ro.fortech.peaa.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ro.fortech.peaa.domain.Job;
import ro.fortech.peaa.service.api.IJobIdentityMap;

public class JobIdentityMap implements IJobIdentityMap {

	private Map<String, Job> map;
	
	protected Log logger = LogFactory.getLog(getClass());
	
	public void setMap(Map<String, Job> map) {
		this.map = map;
	}

	/*
	 * (non-Javadoc)
	 * @see ro.fortech.peaa.service.api.IJobIdentityMap#get(java.lang.String)
	 */
	public Job get(String id) throws Exception {
		logger.info("get job with id " + id + " from Identity Map");
		Job job = null;
		job = map.get(id);
		return job;
	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IEmployeeIdentityMap#put(ro.fortech.peaa.domain.Employee)
	 */
	public void put(Job job) throws Exception {
		logger.info("Put job with id " + job.getId() + " to Identity Map");
		map.put(job.getId(), job);
	}

	/*
	 * (non-Javadoc)
	 * @see ro.fortech.peaa.service.api.IEmployeeIdentityMap#remove(ro.fortech.peaa.domain.Employee)
	 */
	public void remove(Job job) throws Exception {
		logger.info("Remove job with id " + job.getId() + " from Identity Map");
		map.remove(job.getId());
	}

	@Override
	public String toString() {
		String result = "";
		Iterator<Job> it = map.values().iterator();
		result += "Job Identity Map\nID\tTitle\tMinSalary\tMaxSalary\n";
		while (it.hasNext()) {
			Job job = it.next();
			result += job.getId() + "\t" + job.getTitle();
			if (job.getMaxSalary() != null) {
				result += "\t" + job.getMinSalary() + "\t" + job.getMaxSalary();
			}
			result += "\n";
		}
		return result;	
	}

	@Override
	public Integer size() throws Exception {
		return map.size();
	}

	@Override
	public List<Job> getAll() throws Exception {
		List<Job> jobs = new ArrayList<Job>(map.values());
		return jobs;
	}

}
